package com.ngkj.zmfptback.business.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.sys.initData.servicie.read.AreaService;
import com.ngkj.zmfptback.sys.http.OkHttpCli;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ChenHN
 * @since 2021/12/29 creation
 **/
@RestController
@RequestMapping("/gaode")
public class GaoDeiApi {

    @Resource
    private OkHttpCli okHttpCli;


    @Resource
    private AreaService areaService;

    private static  String key = "ce1780ba7065b39a4794b9a5a478c095";

    /**
     * 关键词搜索
     * @param qywzid 区域id
     * @param keywords 关键词
     * @return
     */
    @GetMapping("/getInfoByKeywords")
    public CommonReslut getCenterAndBusiness(String qywzid, String keywords,String types) {
        Map<String, Object> areaById = areaService.getAreaById(qywzid);
        String cityCode = (String) areaById.get("cityCode");
        Map<String, String> map = new HashMap<>();
        map.put("key", key);
        map.put("types",types);
        map.put("keywords", keywords);
        map.put("city", cityCode);
        map.put("extensions", "all");
        StringBuilder stringBuilder = new StringBuilder("https://restapi.amap.com/v3/place/text");
        String result = okHttpCli.doGet(stringBuilder.toString(), map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (((String) jsonObject.get("status")).equals("1")) {
            JSONArray pois = jsonObject.getJSONArray("pois");
            return new CommonReslut(ResultEnum.ok, pois);
        } else {
            return new CommonReslut<>(ResultEnum.error);
        }
    }


    @GetMapping("/getSurround")
    public CommonReslut getSurround(String location,String keywords){
        Map<String,String> map = new HashMap<>();
        map.put("location",location);
        map.put("keywords",keywords);
        map.put("radius","2000");
        map.put("key", key);
        StringBuilder stringBuilder = new StringBuilder("https://restapi.amap.com/v5/place/around");
        String result = okHttpCli.doGet(stringBuilder.toString(), map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (((String) jsonObject.get("status")).equals("1")) {
            JSONArray pois = jsonObject.getJSONArray("pois");
            return new CommonReslut(ResultEnum.ok, pois);
        } else {
            return new CommonReslut<>(ResultEnum.error);
        }
    }
}
